package com.code;


public class PropertyBuilderOfTime implements PropertyBuilder {

	@Override
	public short[] buildProperty(int code, int len,String value) {
		short [] dateByte = new short[3];
		dateByte[0]=(short)code;
		int lvalue=0;
		lvalue=Integer.parseInt(value.substring(0,1))*16+Integer.parseInt(value.substring(1,2));
		dateByte[1]=(short)lvalue;
		lvalue=Integer.parseInt(value.substring(3,4))*16+Integer.parseInt(value.substring(4,5));
		dateByte[2]=(short)lvalue;
		return dateByte;
	}

	@Override
	public String translateData(byte[] datas, int len, int offset) {
		String time=String.format("%02X",datas[offset+1])+":"+String.format("%02X",datas[offset+2]);
		return time;
	}

	@Override
	public String encode(String pvalue, int length) {
		return pvalue.replace(":", "");
	}

	@Override
	public String decode(String pdata, int length) {
		StringBuffer sb=new StringBuffer();
		switch (length){
		case 2:sb.append(pdata.substring(0, 2)).append(":").append(pdata.substring(2, 4));break;
		case 3:sb.append(pdata.substring(0, 2)).append(":").append(pdata.substring(2, 4)).append(":").append(pdata.substring(4, 6));break;
		}
		
		return sb.toString();
	}

	@Override
	public String parseValue(String pvalue) {
		return pvalue;
	}

}
